using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Web.Script.Serialization;

namespace JustinAngel.MetroBlog.Services.Data
{
    public partial class Comment
    {
        private string _emailMd5Hash;

        [DataMember]
        public string EmailMD5Hash
        {
            get
            {
                return Md5EncodeText(Email);
            }
            set { _emailMd5Hash = value; }
        }

        private string _clearEmailForNewComment;
        [DataMember]
        public string ClearEmailForNewComment
        {
            get { return _clearEmailForNewComment; }
            set
            {
                _clearEmailForNewComment = value;
                this.Email = value;
            }
        }

        /// <summary>
        /// Retrieves the MD5 encoded hash string of the provided input.
        /// </summary>
        /// <param name="text">The input text.</param>
        /// <returns>Returns the MD5 hash string of the input string.</returns>
        protected string Md5EncodeText(string text)
        {
            StringBuilder sb = new StringBuilder();

            byte[] ss = MD5.Create()
                .ComputeHash(Encoding.UTF8.GetBytes(text));
            foreach (byte b in ss)
            {
                sb.Append(b.ToString("X2"));
            }
            return sb.ToString().ToLower();
        }

        public bool IsValid()
        {
            return !string.IsNullOrEmpty(Email)
                && !string.IsNullOrEmpty(Name)
                && !string.IsNullOrEmpty(HTML);
        }
    }
}